<?php
// Init owncloud

require_once(OC_App::getAppPath('jobposition') . "/lib/positions.php");

OCP\JSON::callCheck();
if(!OC_User::isLoggedIn()) {
   echo '您需要先登录';
   exit;
} else {
   if (PFactory::isJobseekerUser()) {
      echo "您无权访问该资源！";
      exit;
   }
}

$source = JRequest::getString('source', '');
$position_type = JRequest::getString('position_type', 0);
$position_id = JRequest::getInt('position_id', 0);

$model = new PJobpositionModelPositions();
$lists = $model->getAllJobPositions($source);

$position = array();
foreach ($lists as $p){
   $categroy[$p['cid']] = $p['category'];
   $spec[$p['cid']][$p['sid']] = $p['specialization'];
   $position[$p['cid']][$p['sid']][$p['pid']] = $p['position'];
   if($p['pid'] == $position_id){
       $position_spec_id = $p['sid'];
   }
}
?>
      <div class="dialogp_content">
      <input type="hidden" id="select_pos_type" value="<?=$position_type?>">
      <input type="hidden" id="select_source" value="<?=$source?>">
      <?php
      foreach ($categroy as $ckey => $cvalue){
         echo '<div class="selectwork_table selectwork_table_bgcolor medium">
            <table width="100%" border="0" cellpadding="0" cellspacing="0">
               <tr>
                  <td width="150" style="padding:0 7px;"><b>'.$cvalue.'</b></td>
                  <td>
                     <div class="selectwork_tableright">';
         
         $speclist = array_chunk($spec[$ckey],3,true);
         foreach ($speclist as $slist){
            echo '<ul class="clearfix">';
            foreach ($slist as $skey => $svalue) {
               echo '<li class=""><a href="javascript:;" class="selectwork" id="selectwork_'.$skey.'" data="{\'ckey\':\''.$ckey.'\',\'skey\':\''.$skey.'\',\'svalue\':\''.$svalue.'\'}">'.$svalue.'</a></li>';
            }
            echo '</ul>';
         }
         echo '</div>
                  </td>
               </tr>
            </table>
         </div>';
         }
      ?>
      </div>

      <script type="text/javascript">
      <?php echo "var position = JSON.parse('".json_encode($position)."');";?>
      <?php echo isset($position_spec_id) ? '$("#selectwork_'.$position_spec_id.'").addClass("selected");' : ''?>
      var select_work_id = $('#<?=$position_type?>').val();
      var select_work_txt = $('#<?=$position_type?>_text').val();
      $('#select_work_id').val(select_work_id);
      $('#select_work_text').val(select_work_txt);
      $(".selectwork_dialog").on("click","a.selectwork",function(){
         var target = $(this);
         $.metadata.setType("attr", "data");
         var result  = '<i></i>' +
                       '<a href="javascript:;" id="selectworkconfirm" class="submitbtn_a btn">确认</a>' +
                       '<i class="close_ibtn">&times;</i>' +
                       '<table width="100%" border="0" cellpadding="0" cellspacing="0">' +
                       '<tr>' +
                       '<td width="100%" height="26"><b>'+target.metadata().svalue+'</b></td>' +
                       '</tr>' +
                       '</table>' +
                       '<table width="100%" border="0" cellpadding="0" cellspacing="0">';
         var i = 1;
         var selected = '';
         $.each(position[target.metadata().ckey][target.metadata().skey],function(key,value){
             selected = ($('#select_work_id').val() == key) ? 'selected' : ''
             if(i%2 == 1){
                 result += '<tr><td width="50%"><a class="selectposition '+ selected +'" href="javascript:;" data="{\'sid\':\''+target.metadata().skey+'\',\'pid\':\''+key+'\',\'text\':\''+value+'\'}" >'+value+'</a></td>';
             }else if(i%2 == 0){
                 result += '<td width="50%"><a class="selectposition '+ selected +'" href="javascript:;" data="{\'sid\':\''+target.metadata().skey+'\',\'pid\':\''+key+'\',\'text\':\''+value+'\'}" >'+value+'</a></td></tr>';
             }
             i++;
         });

         if(position[target.metadata().ckey][target.metadata().skey].length % 2 == 1){
             result += '<td width="50%"></td></tr>'
         }

         var pos = {
            my:"left top+10",
            at:"left-10 bottom",
            of:target,
            collision:"none"
         }

         $(".selectwork_listdialog").html(result).show().position(pos).data("position", pos);

      }).children(".modal-body").on("scroll", function() {
         var dialog = $(".selectwork_listdialog");
         var pos = dialog.data("position"); 
         if(!pos) return; 
         var offset = pos.of.offset();
         var moffset = $(this).offset(); 

         if(moffset.top > (offset.top + pos.of.height()) || offset.top > (moffset.top+$(this).height()))
            dialog.css('left', '-1000px');
         else 
            dialog.position(pos);
      }); 


      $('#selectwork_dialog').on('hide', function (){
         $(".selectwork_listdialog").hide().removeData('position');
      });
      
      $('.selectwork_listdialog').delegate('.selectposition', 'click', function(){
         var target = $(this);
         $('#selectwork_dialog').find(".selectwork").removeClass("selected");
         target.closest("table").find(".selectposition").removeClass("selected");
         target.addClass("selected");
         $.metadata.setType("attr", "data");
         $('#selectwork_'+target.metadata().sid).addClass("selected");
         $('#select_work_id').val(target.metadata().pid);
         $('#select_work_text').val(target.metadata().text);
      });
      </script>
